package com.woniu.service.impl;

import com.woniu.entity.CourseTableUtile;
import com.woniu.mapper.ClassDao;
import com.woniu.mapper.CourseDao;
import com.woniu.mapper.CoursetableDao;
import com.woniu.pojo.Class;
import com.woniu.pojo.Course;
import com.woniu.pojo.Coursetable;
import com.woniu.pojo.CoursetableExample;
import com.woniu.service.CoursetableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class CoursetableServiceImpl implements CoursetableService {
    @Autowired
    CoursetableDao coursetableDao;
    @Autowired
    CourseDao courseDao;
    @Autowired
    ClassDao classDao;

    @Override
    public void add(Coursetable coursetable) {
        if(coursetable.getCtWeek()==0){
            coursetable.setCtWeek(null);
        }
        if(coursetable.getCtTime()==0){
            coursetable.setCtTime(null);
        }
        coursetableDao.insert(coursetable);
    }

    @Override
    public void deleteById(Integer id) {
        coursetableDao.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        for (Integer id:ids) {
            coursetableDao.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Coursetable findOne(Integer id) {
        Coursetable coursetable = coursetableDao.selectByPrimaryKey(id);
        return coursetable;
    }

    /**
     *
     * @param classId
     * @return
     */
    @Override
    public List<Coursetable> findByClassId(Integer classId) {
        Coursetable coursetable=new Coursetable();
        coursetable.setClassId(classId);
        List<Coursetable> coursetables = coursetableDao.selectByExample1(coursetable);
        return coursetables;
    }

    /**
     * @author
     * 通过班级ID得到班级课程
     * @param classId
     * @return
     */
    public List<CourseTableUtile> getClassCourse(Integer classId){
        List<Coursetable> coursetables = findByClassId(classId);

        List<CourseTableUtile> courseTableUtiles=new ArrayList<>();
        for(int i =0; i< 5; i++){
            courseTableUtiles.add(new CourseTableUtile());
        }
        for (Coursetable e : coursetables) {
            if (e.getCtTime()!=null&&e.getCtWeek()!=null) {
                switch (e.getCtWeek()) {
                    case 1:
                        courseTableUtiles.get(e.getCtTime()-1).setMonday(e.getConame());
                        break;
                    case 2:
                        courseTableUtiles.get(e.getCtTime()-1).setTuesday(e.getConame());
                        break;
                    case 3:
                        courseTableUtiles.get(e.getCtTime()-1).setWednesday(e.getConame());
                        break;
                    case 4:
                        courseTableUtiles.get(e.getCtTime()-1).setThursday(e.getConame());
                        break;
                    case 5:
                        courseTableUtiles.get(e.getCtTime()-1).setFriday(e.getConame());
                        break;
                    default:
                        break;
                }
            }
        }
        return courseTableUtiles;
    }

    @Override
    public void updateById(Coursetable coursetable) {
        coursetableDao.updateByPrimaryKeySelective(coursetable);
    }

    @Override
    public List<Coursetable> selectByExample(Coursetable coursetable) {
        CoursetableExample example = new CoursetableExample();
        CoursetableExample.Criteria criteria = example.createCriteria();
        if (coursetable.getClassId()!=null) {
            criteria.andClassIdEqualTo(coursetable.getClassId());
        }
        if (coursetable.getCourseId()!=null) {
            criteria.andCourseIdEqualTo(coursetable.getCourseId());
        }
        if (coursetable.getCtWeek()!=null) {
            criteria.andCtWeekEqualTo(coursetable.getCtWeek());
        }
        List<Coursetable> coursetableList = coursetableDao.selectByExample(example);
        return coursetableList;
    }

    @Override
    public List<Coursetable> selectAll() {
        return coursetableDao.selectByExample(null);
    }

    @Override
    public List<Course> selectCourseAll() {
        List<Course> courseList = courseDao.selectByExample(null);
        return courseList;
    }

    @Override
    public List<Class> selectClassAll() {
        List<Class> classList = classDao.selectByExample(null);
        return classList;
    }

    @Override
    public List<Coursetable> selectByExample1(Coursetable coursetable) {
        CoursetableExample example = new CoursetableExample();
        CoursetableExample.Criteria criteria = example.createCriteria();
        if (coursetable.getClassId()!=null&&coursetable.getClassId()!=0) {
            criteria.andClassIdEqualTo(coursetable.getClassId());
        }
        if (coursetable.getCourseId()!=null) {
            criteria.andCourseIdEqualTo(coursetable.getCourseId());
        }
        if (coursetable.getCtWeek()!=null) {
            criteria.andCtWeekEqualTo(coursetable.getCtWeek());
        }
        return coursetableDao.selectByExample1(coursetable);
    }


}
